<?php

class IndexController extends MController
{
	public function actions()
	{
		return array(
			'captcha' => array(
				'class' => 'CCaptchaAction',
				'minLength' => 5,
				'maxLength' => 5,
				'width' => 65,
				'height' => 28,
				'padding' => 0,
				'offset' => 0
			)
		);
	}
	/*
	public function filters()
	{
		return array_merge(
			parent::filters(), 
			array(
				array(
					'application.controllers.user.LoginFilter + user_login',
					'var_name' => 'var'
				)
			)
		);
	}
	*/
		
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionLogin()
	{
		if (!Yii::app()->user->isGuest) 
		{
			$this->redirect(BASE_URL);
		}
		
		$request =& Yii::app()->request;
		if ($request->isAjaxRequest)
		{
			$admin_name = $request->getPost('admin_name');
			$admin_pass = $request->getPost('admin_pass');
			$verifycode = $request->getPost('verifycode');
			$captcha = new CCaptchaAction($this, 'captcha');
			if (strtolower($verifycode) != $captcha->getVerifyCode())
			{
				$this->util->return_json('验证码错误', false);
			}
			else
			{
				$identity = new UserIdentity($admin_name, $admin_pass);
				if($user = $identity->authenticate())
				{
					$AdminModel = new AdminModel();
					$AdminModel->login($identity, $user);
					$this->util->return_json('', true, BASE_URL);
				}
				else
				{
					$this->util->return_json('账号或密码错误', false);
				}
			}
		}
		elseif (Yii::app()->user->isGuest)
		{
			$this->foot_js[] = 'js/plugins/forms/jquery.validationEngine-zh_CN.js';
			$this->foot_js[] = 'js/plugins/forms/jquery.validationEngine.js';
			$this->foot_js[] = 'js/login.js';
			$this->render('login');
		}
	}
	
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->user->loginUrl);
	}
	
	public function actionError()
	{
		if($error = Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
			{
				echo $error['message'];
			}
			else
			{
				$this->render('error', $error);
			}
		}
	}
}